package com.zahb.qadx.ui.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.CertificateBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.DisplayUtil;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zahb.qadx.util.QRCodeUtil;
import com.zahb.qadx.util.Tips;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 证书详情
 */
public class CertificateDetailActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivValidate)
    ImageView ivValidate;
    @BindView(R.id.ivLogo)
    ImageView ivLogo;
    @BindView(R.id.ivSeed)
    ImageView ivSeed;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvPosition)
    TextView tvPosition;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvDate)
    TextView tvDate;
    @BindView(R.id.tvSource)
    TextView tvSource;
    @BindView(R.id.tvNumOne)
    TextView tvNumOne;
    @BindView(R.id.rlTop)
    RelativeLayout rlTop;
    @BindView(R.id.ivQrcode)
    ImageView ivQrcode;
    @BindView(R.id.tvCertificatenName)
    TextView tvCertificatenName;
    @BindView(R.id.tvInvadateDate)
    TextView tvInvadateDate;

    @Override
    protected int getLayout() {
        return R.layout.activity_certificate_detail;
    }

    @Override
    protected void initViews() {

        getCertificate();
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.certificate_detail;
    }


    private void getCertificate() {

        addDisposable(RetrofitService.getNetService()
                .getCertificate(getIntent().getIntExtra("data", 0))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        CertificateBean certificateBean = response.getData();
                        tvTitle.setText(certificateBean.getCertificateName());
                        tvCertificatenName.setText(certificateBean.getCertificateName());

                        tvNumOne.setText("证书编号：" + certificateBean.getCertificateCode());
                        if (!TextUtils.isEmpty(certificateBean.getLogo()))
                            ImageLoaderKt.loadImage(ivLogo, certificateBean.getLogo());


                        tvName.setText(certificateBean.getUserName());
                        //tvPosition.setText(certificateBean.);
                        tvDes.setText("    " + certificateBean.getDesc());
                        tvNum.setText("证书编号：" + certificateBean.getCertificateCode());

                        tvDate.setText("颁发日期：" + splittime(certificateBean.getIssueDate()));
                        tvInvadateDate.setText("失效日期："+splittime(certificateBean.getInvalidDate()));
                        tvSource.setText("颁发机构：" + certificateBean.getCertificationAuthority());

                        if (!TextUtils.isEmpty(certificateBean.getSealUrl()))
                            ImageLoaderKt.loadImage(ivSeed, certificateBean.getSealUrl());

                        int width = DisplayUtil.dip2px(60f);
                        int height = DisplayUtil.dip2px(60f);
                        Bitmap mQrCodeBitmap = QRCodeUtil.createQRCodeBitmap(
                                certificateBean.getQrCode(), width, height, "UTF-8", "H", "0",
                                Color.BLACK, Color.WHITE, null, 0.2f, null);
                        ivQrcode.setImageBitmap(mQrCodeBitmap);

                        if (certificateBean.getInvalidStatus() == 2) {
                            ivValidate.setImageResource(R.mipmap.certificate_invalidate);
                            rlTop.setBackgroundResource(R.drawable.corner8_gray);
                            tvCertificatenName.setTextColor(getResources().getColor(R.color.invalidate));
                            tvName.setTextColor(getResources().getColor(R.color.invalidate));
                            tvPosition.setTextColor(getResources().getColor(R.color.invalidate));
                            tvDes.setTextColor(getResources().getColor(R.color.invalidate));
                            tvNum.setTextColor(getResources().getColor(R.color.invalidate));
                            tvDate.setTextColor(getResources().getColor(R.color.invalidate));
                            tvSource.setTextColor(getResources().getColor(R.color.invalidate));
                            tvInvadateDate.setTextColor(getResources().getColor(R.color.invalidate));
                        }


                    } else if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    hideProgressDialog();
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));
    }

    public static String splittime(String time) {
        String[] times = time.split("T");
        return times[0];
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
